#include <iostream>
#include <algorithm>

using namespace std;
const int N = 100010, M = 200010;
int n, m;
int p[N];
struct Edge
{
    int a, b, c;
    bool operator<(const Edge &W)
    {
        return c < W.c;
    }
} edges[M];

int find(int x)
{
    if (p[x] != x)
    {
        return p[x] = find(p[x]);
    }
    return x;
}

int main()
{
    cin >> n >> m;
    for (int i = 0; i < m; ++i)
    {
        int a, b, c;
        scanf("%d %d %d", &a, &b, &c);
        edges[i] = {a, b, c};
    }
    for (int i = 1; i <= n; ++i)
    {
        p[i] = i;
    }
    int ret = 0, cnt = 0;
    sort(edges, edges + m);
    for (int i = 0; i < m; ++i)
    {
        int a = edges[i].a, b = edges[i].b, c = edges[i].c;
        a = find(a);
        b = find(b);
        if (a != b)
        {
            p[a] = b;
            ret += c;
            cnt++;
        }
    }
    if (cnt < n - 1)
    {
        printf("impossible");
    }
    else
    {
        printf("%d", ret);
    }
    return 0;
}